package com.herench.bs.transfer;

import com.herench.bs.transfer.Dialect;
import org.apache.cxf.endpoint.Client;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import java.util.HashMap;
import java.util.Map;

/**
 * com.herench.bs.transfer.transfer
 * Created by wangwenkai on 2017/10/11/15:49
 * E-mail：605064334@qq.com
 * <p>
 * 查询是否可支付
 */
public class CheckAppointmentStateInsuranceTransfer extends AbstractTransfer {

    public CheckAppointmentStateInsuranceTransfer(String method, Client client, String messageMethod) {
        super(method, client,messageMethod);
    }

    @Override
    public String toInvokeMessage(Map<String, Object> params) throws Exception {
        String hisClinicAppointsId = (String) params.get("hisClinicAppointsId");
        String hospitalCode = (String) params.get("hospitalCode");
        String idCard = (String) params.get("idCard");
        String patientId = (String) params.get("patientId");
        //A普通患者
        String identity = "A";
        String chargeType = (String) params.get("chargeType");
        String insurNo = (String) params.get("insurNo");
        String insurPwd = "";
        String insuranceType = (String) params.get("insuranceType");
        String payType = (String) params.get("payType");
        String mtType = (String) params.get("mtType");
        String syjyxm = (String) params.get("syjyxm");
        String bcgsbm = (String) params.get("bcgsbm");
        Document document = getTplDoc(getMethod());
        Element dataRowRegist = document.getRootElement().element("CommitData").addElement("Regist").addElement("DataRow");
        dataRowRegist.addAttribute("APPOINT_ID", hisClinicAppointsId).addAttribute("PERSON_NO", idCard).addAttribute("PATIENT_ID", patientId);
        dataRowRegist.addAttribute("IDENTITY", identity).addAttribute("CHARGE_TYPE", chargeType);
        Element dataRowInsurance = document.getRootElement().element("CommitData").addElement("Insurance").addElement("DataRow");
        dataRowInsurance.addAttribute("INSUR_NO", insurNo).addAttribute("INSUR_PWD", insurPwd).addAttribute("INSURANCE_TYPE", insuranceType);
        dataRowInsurance.addAttribute("PAY_TYPE", payType).addAttribute("MT_TYPE", mtType).addAttribute("SY_JYXM", syjyxm).addAttribute("BCGSBM", bcgsbm);
        return document.asXML();
    }

    @Override
    public Map<String, Object> toResultMessage(String resMsg) throws Exception {
        Map<String, Object> resMap = new HashMap<>(16);
        Document document = DocumentHelper.parseText(resMsg);
        Element resultInfo = document.getRootElement().element("Result").element("Info");
        String executeStatus = resultInfo.attributeValue("EXECUTE_STATUS");
        String executeMessage = resultInfo.attributeValue("EXECUTE_MESSAGE");
        if (Dialect.getSuccessfulState().equals(executeStatus)) {
            Element dataRow = document.getRootElement().element("ReturnData").element("Data").element("DataRow");
            CheckAppointmentStateInnerMessage dataRowMessage = new CheckAppointmentStateInnerMessage(dataRow);
            resMap.put("visitNo", dataRowMessage.getVisitNo());
            resMap.put("visitDate", dataRowMessage.getVisitDate());
            resMap.put("rcptNo", dataRowMessage.getRcptNo());
            resMap.put("serialNo", dataRowMessage.getSerialNo());
            resMap.put("unVisitCount", dataRowMessage.getUnVisitCount());
            resMap.put("registFee", dataRowMessage.getRegistFee());
            resMap.put("clinicFee", dataRowMessage.getClinicFee());
            resMap.put("otherFee", dataRowMessage.getOtherFee());
            resMap.put("clinicCharge", dataRowMessage.getClinicCharge());
            resMap.put("tranNo", dataRowMessage.getTranNo());
            resMap.put("insurVisitId", dataRowMessage.getInsurVisitId());
            resMap.put("insurPay", dataRowMessage.getInsurPay());
            resMap.put("accountPay", dataRowMessage.getAccountPay());
            resMap.put("ownPay", dataRowMessage.getOwnPay());
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
        } else {
            resMap.put("code", executeStatus);
            resMap.put("message", executeMessage);
        }
        return resMap;
    }

    private class CheckAppointmentStateInnerMessage implements InnerMessage {

        /**
         * 就诊号
         */
        private String visitNo;
        /**
         * 挂号日期
         */
        private String visitDate;
        /**
         * 内部票据号
         */
        private String rcptNo;
        /**
         * 就诊顺序号
         */
        private String serialNo;
        /**
         * 未就诊人数
         */
        private String unVisitCount;
        /**
         * 挂号费
         */
        private String registFee;
        /**
         * 诊查费
         */
        private String clinicFee;
        /**
         * 其他费用
         */
        private String otherFee;
        /**
         * 总费用
         */
        private String clinicCharge;
        /**
         * 合同号
         */
        private String tranNo;
        /**
         * 医保就诊顺序号
         */
        private String insurVisitId;
        /**
         * 统筹支付
         */
        private String insurPay;
        /**
         * 个人账户支付
         */
        private String accountPay;
        /**
         * 个人自费
         */
        private String ownPay;



        public CheckAppointmentStateInnerMessage(Element element) {

            this.visitNo = element.attributeValue("VISIT_NO");
            this.visitDate = element.attributeValue("VISIT_DATE");
            this.rcptNo = element.attributeValue("RCPT_NO");
            this.serialNo = element.attributeValue("SERIAL_NO");
            this.unVisitCount = element.attributeValue("UN_VISIT_COUNT");
            this.registFee = element.attributeValue("REGIST_FEE");
            this.clinicFee = element.attributeValue("CLINIC_FEE");
            this.otherFee = element.attributeValue("OTHER_FEE");
            this.clinicCharge = element.attributeValue("CLINIC_CHARGE");
            this.tranNo = element.attributeValue("TRAN_NO");
            this.insurVisitId = element.attributeValue("INSUR_VISIT_ID");
            this.insurPay = element.attributeValue("INSUR_PAY");
            this.accountPay = element.attributeValue("ACCOUNT_PAY");
            this.ownPay = element.attributeValue("OWN_PAY");
        }

        public String getVisitNo() {
            return visitNo;
        }

        public String getVisitDate() {
            return visitDate;
        }

        public String getRcptNo() {
            return rcptNo;
        }

        public String getSerialNo() {
            return serialNo;
        }

        public String getUnVisitCount() {
            return unVisitCount;
        }

        public String getRegistFee() {
            return registFee;
        }

        public String getClinicFee() {
            return clinicFee;
        }

        public String getOtherFee() {
            return otherFee;
        }

        public String getClinicCharge() {
            return clinicCharge;
        }

        public String getTranNo() {
            return tranNo;
        }

        public String getInsurVisitId() {
            return insurVisitId;
        }

        public String getInsurPay() {
            return insurPay;
        }

        public String getAccountPay() {
            return accountPay;
        }

        public String getOwnPay() {
            return ownPay;
        }
    }
}
